package com.jrx.anytxn.transaction.job.marketingFile.config;

import com.jrx.anytxn.customer.entity.TlMarketingRecord;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.job.marketingFile.step.*;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * 生成每日营销文件
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
@EnableBatchProcessing
public class GenerateMarketingFileConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private MarketingDetailFileProcessor marketingDetailFileProcessor;
    @Autowired
    private MarketingDetailFileWriter marketingDetailFileWriter;
    @Autowired
    private MarketingSumFileProcessor marketingSumFileProcessor;
    @Autowired
    private MarketingSumFileWriter marketingSumFileWriter;
    @Autowired
    private GenerateMarketingFileListener generateMarketingFileListener;

    @Bean
    public Job generateMarketFileJob() {
        return jobBuilderFactory.get("generateMarketFileJob").incrementer(new RunIdIncrementer()).listener(generateMarketingFileListener)
                .start(generateMarketSummaryFileStep()) //生成营销汇总文件
                .next(generateMarketDetailFileStep())   //生成营销明细文件
                .build();
    }

    @Bean
    public Step generateMarketSummaryFileStep(){
        return stepBuilderFactory.get("generateMarketSummaryFileStep")
                .<String, String>chunk(anytxnBatchProperties.getBatchMarketingFileConfiguration().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(marketSummaryFileReader())
                .processor(marketingSumFileProcessor)
                .writer(marketingSumFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getBatchMarketingFileConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<String> marketSummaryFileReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new MarketingSumFileReader(sqlSessionFactory, org,anytxnBatchProperties.getBatchMarketingFileConfiguration().getPageSize());
    }

    @Bean
    public Step generateMarketDetailFileStep(){
        return stepBuilderFactory.get("generateMarketDetailFileStep")
                .<TlMarketingRecord, String>chunk(anytxnBatchProperties.getBatchMarketingFileConfiguration().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(marketingDetailFileReader())
                .processor(marketingDetailFileProcessor)
                .writer(marketingDetailFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getBatchMarketingFileConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<TlMarketingRecord> marketingDetailFileReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new MarketingDetailFileReader(sqlSessionFactory, org,anytxnBatchProperties.getBatchMarketingFileConfiguration().getPageSize());
    }
}
